package com.wyzz.hr.action;

import com.opensymphony.xwork2.Action;
import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.base.UsersAuthority;
import com.wyzz.hr.bo.AuthBo;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Authority;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

public class GetUserSessionAction extends BaseAction implements Action,
		Constants {

	public GetUserSessionAction() {
	}

	public String execute() throws Exception {
		HttpSession session = getSession();
		UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
		if (session.getAttribute("userId") != null) {
			user = userBo.getUserByName(session.getAttribute("userId")
					.toString());
			if (user == null)
				return "noauth";
		} else {
			user = userBo.getUserByName(user.getUiUsername());
			if (user == null)
				return "input";
		}
		AuthBo authBo = (AuthBo) SpringBeanFactory.getBean("authService");
		List list = authBo.findAuthByIdList(user.getUiAuthIntegerList());
		for (int i = 0; i < list.size(); i++)
			System.out.println(list.get(i));
		HashMap authMap = getUserSesionMap(authBo.findAuthByIdList(user
				.getUiAuthIntegerList()));

		session.setAttribute("authList", authMap);
		session.setAttribute("userinfo", user);
		IEmployeeBo empBo = (IEmployeeBo) SpringBeanFactory.getBean("empBo");
		session.setAttribute("empName", empBo.loadEmp(user.getId(), null)
				.getEmpName());
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		session.setAttribute("positionId", posBo.getPosByEmpNo(user.getId(),
				new String[0]).getId());
		String requiredURL = (String) session.getAttribute("requiredURL");
		if (requiredURL != null && requiredURL.length() > 0) {
			session.removeAttribute("requiredURL");
			ServletActionContext.getResponse().sendRedirect(requiredURL);
			return null;
		} else {
			return "success";
		}
	}

	private HashMap getUserSesionMap(List inputAuthorityList) {
		if (inputAuthorityList == null || inputAuthorityList.size() == 0)
			return new HashMap();
		HashMap returnHashMap = new HashMap();
		Iterator authorityIterator = inputAuthorityList.iterator();
		Authority tempAuthority = null;
		for (; authorityIterator.hasNext(); returnHashMap.put(
				new UsersAuthority(tempAuthority.getAuthorityModuleNo().trim(),
						new String[] { (new StringBuilder()).append(
								tempAuthority.getAuthorityConditionNo())
								.append("").toString() }), tempAuthority
						.getAuthorityModuleNo().trim().substring(0, 1)))
			tempAuthority = (Authority) authorityIterator.next();
		return returnHashMap;
	}

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	private static final long serialVersionUID = 1L;
	private Userinfo user;
}
